package com.ruoyi.finance.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.finance.service.IAccountService;
import com.ruoyi.system.api.domain.finance.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 账户Controller
 *
 * @author ruoyi
 * @date 2024-10-25
 */
@RestController
@RequestMapping("/account")
public class AccountController extends BaseController {
    @Autowired
    private IAccountService accountService;

    /**
     * 查询账户列表
     */
    @RequiresPermissions("finance:account:list")
    @GetMapping("/list")
    public TableDataInfo list(Account account) {
        startPage();
        List<Account> list = accountService.selectAccountList(account);
        return getDataTable(list);
    }

    /**
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 查询所有账户列表
     * @date 2024/11/11 下午11:23
     **/
    @RequiresPermissions("finance:account:list")
    @GetMapping("/listAll")
    public AjaxResult listAll() {
        List<Account> list = accountService.selectAccountList(new Account());
        return AjaxResult.success(list);
    }

    /**
     * 导出账户列表
     */
    @RequiresPermissions("finance:account:export")
    @Log(title = "账户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Account account) {
        List<Account> list = accountService.selectAccountList(account);
        ExcelUtil<Account> util = new ExcelUtil<Account>(Account.class);
        util.exportExcel(response, list, "账户数据");
    }

    /**
     * 获取账户详细信息
     */
    @RequiresPermissions("finance:account:query")
    @GetMapping(value = "/{Id}")
    public AjaxResult getInfo(@PathVariable("Id") Long Id) {
        return success(accountService.selectAccountById(Id));
    }

    /**
     * 新增账户
     */
    @RequiresPermissions("finance:account:add")
    @Log(title = "账户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Account account) {
        return toAjax(accountService.insertAccount(account));
    }

    /**
     * 修改账户
     */
    @RequiresPermissions("finance:account:edit")
    @Log(title = "账户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Account account) {
        return toAjax(accountService.updateAccount(account));
    }

    /**
     * 删除账户
     */
    @RequiresPermissions("finance:account:remove")
    @Log(title = "账户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{Ids}")
    public AjaxResult remove(@PathVariable Long[] Ids) {
        return toAjax(accountService.deleteAccountByIds(Ids));
    }
}
